<?php
/**
 * Created by PhpStorm.
 * User: xiandanjia
 * Date: 2019/1/18
 * Time: 下午 6:15
 */

namespace App\Components\rank;


use App\Components\Handle;
use App\Components\Tool;

class Main
{
    const FAMILY = 'family';
    const FAMILYMEMBER = 'family_member';
    const STAR = 'star';
    const USER = 'user';
    /**
     * 排行榜刷新
     * @param $time 时间戳
     * @param $uid  uid
     * @param $sid  主播id
     * @param $amount   金额
     * @param $fid  家族id
     * @return bool
     */
    public static function init($time,$uid,$sid,$amount,$fid){
        $day = self::getDay($time);
        $week = self::getWeek($time);
        $month = self::getMonth($time);
        $amount = round($amount,2);
        $type = [self::USER,self::STAR];
        Tool::log($amount);
        if ($fid){
            $type[] = self::FAMILY;
            $type[] = self::FAMILYMEMBER;
        }
        $time_key = [$day,$week,$month];
        $commands = [];
        //土豪榜，用户发红包榜单
        foreach ($time_key as $v){
            $i = [];
            $i['command'] = 'ZINCRBY';//操作有序集合
            $i['key'] = self::getKeys(self::USER,$v);
            $i['val'] = $uid;
            $i['num'] = $amount;
            $commands[] = $i;
        }
        //主播榜，收红包榜单
        foreach ($time_key as $v){
            $i = [];
            $i['command'] = 'ZINCRBY';//操作有序集合
            $i['key'] = self::getKeys(self::STAR,$v);
            $i['val'] = $sid;
            $i['num'] = $amount;
            $commands[] = $i;
        }
        //主播有家族
        if ($fid){
            //家族排行榜
            foreach ($time_key as $v){
                $i = [];
                $i['command'] = 'ZINCRBY';//操作有序集合
                $i['key'] = self::getKeys(self::FAMILY,$v);
                $i['val'] = $fid;
                $i['num'] = $amount;
                $commands[] = $i;
            }
            //家族成员排行榜
            foreach ($time_key as $v){
                $i = [];
                $i['command'] = 'ZINCRBY';//操作有序集合
                $i['key'] = self::getKeys(self::FAMILYMEMBER.'::'.$fid,$v);
                $i['val'] = $sid;
                $i['num'] = $amount;
                $commands[] = $i;
            }
        }

        $redis = Handle::redis()->multi(\Redis::PIPELINE);
        //组成排行榜命令
        foreach ($commands as $v){
            $redis->{$v['command']}($v['key'],$v['num'],$v['val']);
        }
        $redis->exec();
    }

    /**
     * 获取排行榜缓存名称
     * @param $type
     * @param $key
     * @return string
     */
    public static function getKeys($type,$key){
        return "NANYOULI::RANK::{$type}::{$key}";
    }

    /**
     * 给主播，土豪，家族成员加榜
     * @param $type
     * @param $time_key
     * @param $id
     * @param $num 可以为负数
     */
    public static function zAdd($type,$time_key,$id,$num){
        Handle::redis()->ZINCRBY(self::getKeys($type,$time_key),$num,$id);
    }

    /**
     * 清理榜单
     * @param $type
     * @param $time_key
     * @param $id
     */
    public static function ClearRank($type,$time_key,$id){
        Handle::redis()->zDelete(self::getKeys($type,$time_key),$id);
    }

    /**
     * 获取日期
     * @param $time
     * @return false|string
     */
    public static function getDay($time){
        return date('Ymd',$time);
    }

    /**
     * 获取周数
     * @param $time
     * @return false|string
     */
    public static function getWeek($time){
        return date('W',$time);
    }

    /**
     * 获取排行榜数据
     * @param $type
     * @param $time_key
     * @param $limit
     * @return mixed
     */
    public static function RankDesc($type,$time_key,$limit){
        $rank = Handle::redis()->zrevrange(self::getKeys($type,$time_key),0,$limit,'withscores');
        if (!$rank){
            return [];
        }
        $list = [];
        $key = '';
        foreach ($rank as $k=> $v){
            if ($k%2 == 0){
                $key = $v;
            }else{
                $list[$key] = $v;
            }
        }
        return $list;
    }
    /**
     * 获取月份
     * @param $time
     * @return false|string
     */
    public static function getMonth($time){
        return date('Ym',$time);
    }

    /**
     * 清除过期的缓存Key
     * @return array
     */
    public static function ClearTimeoutKey(){
        $day = self::getDay(time()-86400*3);
        $week = self::getDay(time()-86400*7*3);
        $month = self::getDay(time()-86400*31*2);
        $arr = Handle::redis()->keys("NANYOULI::RANK::*::{$day}");
        $arr2 = Handle::redis()->keys("NANYOULI::RANK::*::{$week}");
        $arr3 = Handle::redis()->keys("NANYOULI::RANK::*::{$month}");
        $all = array_merge($arr,$arr2,$arr3);
        return $all;
    }
}